/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.qhq.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.qhq.entity.School;
import com.thinkgem.jeesite.modules.qhq.dao.SchoolDao;
import com.thinkgem.jeesite.modules.qhq.entity.SchoolDeptlist;
import com.thinkgem.jeesite.modules.qhq.dao.SchoolDeptlistDao;
import com.thinkgem.jeesite.modules.qhq.entity.SchoolParentlist;
import com.thinkgem.jeesite.modules.qhq.dao.SchoolParentlistDao;
import com.thinkgem.jeesite.modules.qhq.entity.SchoolSystemlist;
import com.thinkgem.jeesite.modules.qhq.dao.SchoolSystemlistDao;

/**
 * 学校管理Service
 * @author 秦华权
 * @version 2017-11-27
 */
@Service
@Transactional(readOnly = true)
public class SchoolService extends CrudService<SchoolDao, School> {

	@Autowired
	private SchoolDeptlistDao schoolDeptlistDao;
	@Autowired
	private SchoolParentlistDao schoolParentlistDao;
	@Autowired
	private SchoolSystemlistDao schoolSystemlistDao;
	
	public School get(String id) {
		School school = super.get(id);
		school.setSchoolDeptlistList(schoolDeptlistDao.findList(new SchoolDeptlist(school)));
		school.setSchoolParentlistList(schoolParentlistDao.findList(new SchoolParentlist(school)));
		school.setSchoolSystemlistList(schoolSystemlistDao.findList(new SchoolSystemlist(school)));
		return school;
	}
	
	public List<School> findList(School school) {
		return super.findList(school);
	}
	
	public Page<School> findPage(Page<School> page, School school) {
		return super.findPage(page, school);
	}
	
	@Transactional(readOnly = false)
	public void save(School school) {
		super.save(school);
		for (SchoolDeptlist schoolDeptlist : school.getSchoolDeptlistList()){
			if (schoolDeptlist.getId() == null){
				continue;
			}
			if (SchoolDeptlist.DEL_FLAG_NORMAL.equals(schoolDeptlist.getDelFlag())){
				if (StringUtils.isBlank(schoolDeptlist.getId())){
					schoolDeptlist.setSchool(school);
					schoolDeptlist.preInsert();
					schoolDeptlistDao.insert(schoolDeptlist);
				}else{
					schoolDeptlist.preUpdate();
					schoolDeptlistDao.update(schoolDeptlist);
				}
			}else{
				schoolDeptlistDao.delete(schoolDeptlist);
			}
		}
		for (SchoolParentlist schoolParentlist : school.getSchoolParentlistList()){
			if (schoolParentlist.getId() == null){
				continue;
			}
			if (SchoolParentlist.DEL_FLAG_NORMAL.equals(schoolParentlist.getDelFlag())){
				if (StringUtils.isBlank(schoolParentlist.getId())){
					schoolParentlist.setSchool(school);
					schoolParentlist.preInsert();
					schoolParentlistDao.insert(schoolParentlist);
				}else{
					schoolParentlist.preUpdate();
					schoolParentlistDao.update(schoolParentlist);
				}
			}else{
				schoolParentlistDao.delete(schoolParentlist);
			}
		}
		for (SchoolSystemlist schoolSystemlist : school.getSchoolSystemlistList()){
			if (schoolSystemlist.getId() == null){
				continue;
			}
			if (SchoolSystemlist.DEL_FLAG_NORMAL.equals(schoolSystemlist.getDelFlag())){
				if (StringUtils.isBlank(schoolSystemlist.getId())){
					schoolSystemlist.setSchool(school);
					schoolSystemlist.preInsert();
					schoolSystemlistDao.insert(schoolSystemlist);
				}else{
					schoolSystemlist.preUpdate();
					schoolSystemlistDao.update(schoolSystemlist);
				}
			}else{
				schoolSystemlistDao.delete(schoolSystemlist);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(School school) {
		super.delete(school);
		schoolDeptlistDao.delete(new SchoolDeptlist(school));
		schoolParentlistDao.delete(new SchoolParentlist(school));
		schoolSystemlistDao.delete(new SchoolSystemlist(school));
	}
	
}